#include <QtGui>
#include <QString>

#include "geometryattribewidget.h"
#include "geometrydatanode.h"
#include "../renderer/renderplugin.h"

using namespace PBVP;

GeometryAttribWidget::GeometryAttribWidget(GeometryDataNode *pGeoNode, QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
	
	m_pGeoDataNode = pGeoNode;

	updateAttributes();

	connect(ui.invertNormalBox, SIGNAL(stateChanged(int)), this, SLOT(invertNormals()));
	connect(ui.invertFaceBox, SIGNAL(stateChanged(int)), this, SLOT(invertFaceNormals()));
}

void GeometryAttribWidget::setRenderingProperty(QWidget *renderPropWidget)
{
	if(renderPropWidget) {
		renderPropWidget->setParent(ui.renderPropertyTab);
	}
	renderPropWidget->setGeometry(10, 10, renderPropWidget->width(), renderPropWidget->height());
	if(ui.renderPropertyTab->isVisible()) renderPropWidget->show();
}

void GeometryAttribWidget::updateAttributes()
{
	m_pGeometry = (GeometryData*)m_pGeoDataNode->data();

	QString str;
	ui.numVertexLabel->setText(str.setNum(m_pGeometry->numOfVertices()));
	ui.numTriangleLabel->setText(str.setNum(m_pGeometry->numOfTriangles()));
	ui.numLineLabel->setText(str.setNum(m_pGeometry->numOfLines()));
	ui.numPointLabel->setText(str.setNum(m_pGeometry->numOfPoints()));
	ui.numQuadLabel->setText(str.setNum(m_pGeometry->numOfQuads()));
	if (m_pGeometry->hasNormalArray())
	{
		ui.normalCheckBox->setCheckState(Qt::Checked);
	}
	if (m_pGeometry->hasColorArray())
	{
		ui.colorCheckBox->setCheckState(Qt::Checked);
	}
	if(m_pGeometry->hasTex2DArray())
	{
		ui.tex2DCheckBox->setCheckState(Qt::Checked);
	}
}

//////////////////////////////////////////////////////////////////////////
// Slots
//////////////////////////////////////////////////////////////////////////
void GeometryAttribWidget::invertNormals()
{
	m_pGeometry->invertNormals();
	m_pGeoDataNode->updateView();
}

void GeometryAttribWidget::invertFaceNormals()
{
	m_pGeometry->invertFaceOrients();
	m_pGeoDataNode->updateView();
}